package com.grape.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.grape.constants.Constants;
import com.grape.domain.BaseDomain;
import com.grape.utils.AssertUtil;
import com.grape.utils.StringUtils;
import com.grape.utils.ThreadLocalUtil;

/**
 * 公共查询构造器基类
 * @author  putao
 * @email   2628279194@qq.com
 */
public class BaseWrapper<T> extends QueryWrapper<T> {
    public BaseWrapper() {
        this(null);
    }

    public BaseWrapper(T entity) {
        super.setEntity(entity);
        super.initNeed();
    }

    public BaseWrapper(T entity, String... columns) {
        super.setEntity(entity);
        super.initNeed();
        this.select(columns);
    }

    public QueryWrapper<T> eq(String columnName, String value) {
        AssertUtil.hasText(value, "where条件时值不能为空");
        return super.eq(columnName, value);
    }

    public QueryWrapper<T> init() {
        BaseWrapper<T> wrapper = new BaseWrapper<>();
        return wrapper.eq(Constants.DEL_FLAG, BaseDomain.DEL_FLAG_NORMAL);
    }

    public QueryWrapper<T> initById(String id) {
        BaseWrapper<T> wrapper = new BaseWrapper<>();
        return wrapper.eq(Constants.DEL_FLAG, BaseDomain.DEL_FLAG_NORMAL).eq(StringUtils.isNotBlank(id), Constants.ID, id);
    }

    public QueryWrapper<T> initByIds(String ids) {
        BaseWrapper<T> wrapper = new BaseWrapper<>();
        return wrapper.eq(Constants.DEL_FLAG, BaseDomain.DEL_FLAG_NORMAL).in(StringUtils.isNotBlank(ids), Constants.ID, StringUtils.convertList(ids));
    }

    public QueryWrapper<T> init(String userId) {
        QueryWrapper<T> wrapper = new BaseWrapper<>();
        return wrapper.eq(Constants.DEL_FLAG, BaseDomain.DEL_FLAG_NORMAL).eq(Constants.CREATE_ID, userId);
    }

    public QueryWrapper<T> initUserId(String userId) {
        QueryWrapper<T> wrapper = new BaseWrapper<>();
        return wrapper.eq(Constants.USER_ID, userId);
    }

    public QueryWrapper<T> initDelFlagAndUserId(String userId) {
        QueryWrapper<T> wrapper = new BaseWrapper<>();
        return wrapper.eq(Constants.DEL_FLAG, BaseDomain.DEL_FLAG_NORMAL).eq(Constants.USER_ID, userId);
    }

    public QueryWrapper<T> initDelFlagAndUserId() {
        QueryWrapper<T> wrapper = new BaseWrapper<>();
        return wrapper.eq(Constants.DEL_FLAG, BaseDomain.DEL_FLAG_NORMAL).eq(Constants.USER_ID, ThreadLocalUtil.getUserId());
    }
}
